#include <genrand.h>

static int initGenerator()
{
    static int initflag = 0;
    if (!initflag)
    {
        // ===coding by user self===
        srand(systemNowMs());
    }
    initflag = 1;
    return 0;
}

static int generatorRandString_Common(char *outstr, int outsize, const char *genbuff, int gensize)
{
    int i;
    // logdebug("need size=%d", outsize);
    initGenerator();
    if (outstr == NULL || outsize <= 0)
    {
        logerror("");
        return -1;
    }
    if (genbuff == NULL || gensize <= 0)
    {
        for (i = 0; i < outsize; i++)
        {
            outstr[i] = rand();
        }
        return outsize;
    }
    for (i = 0; i < outsize; i++)
    {
        outstr[i] = genbuff[rand() % gensize];
    }
    return outsize;
}

int generatorRandHexBuffer(char *outstr, int outsize)
{
    return generatorRandString_Common(outstr, outsize, NULL, 0);
}

int generatorRandString_Gigit(char *outstr, int outsize)
{
    static const char MCU_CONST_TYPE tmpbuffer[] = "0123456789";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Hex(char *outstr, int outsize)
{
    static const char MCU_CONST_TYPE tmpbuffer[] = "0123456789ABCDEF";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Aa1(char *outstr, int outsize)
{
    static const char MCU_CONST_TYPE tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}

int generatorRandString_Aa1s(char *outstr, int outsize)
{
    static const char MCU_CONST_TYPE tmpbuffer[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz~!@#$^&*()_+|<>?`;";
    return generatorRandString_Common(outstr, outsize, tmpbuffer, sizeof(tmpbuffer) - 1);
}
